package enumTest;

import com.google.gson.Gson;

/**
 * @author jie
 * @version 创建时间：2017年10月12日 下午8:05:14
 *
 * 结论：测试json 解析 枚举类型，默认值：null
 * // json 可以 name ,不能解析自定义的 dayType.
 */
public class EnumDateType_Json {
    public static void main(String[] args) {
        String type = CustomEnumDateType.getType(CustomEnumDateType.Day);
        System.out.println("CustomEnumDateType.Day==" + CustomEnumDateType.Day);//dayType
        System.out.println("CustomEnumDateType.Day.name()==" + CustomEnumDateType.Day.name());//dayType
        System.out.println("CustomEnumDateType.Day.ordinal()==" + CustomEnumDateType.Day.ordinal());//dayType

        //测试 枚举默认类型
		String jsonEnum = "{\"a\":123,\"myDate\":\"Day\",\"planType\":\"NOW\"}";
//		String jsonEnum = "{\"a\":123,\"myDate\":\"dayType\",\"planType\":\"NOW\"}";//json 可以解析 Day ,不能解析自定义的 dayType.
//        String jsonEnum = "{\"a\":123,\"myDate\":\"Day\"}"; //结论：测试json 解析 枚举类型，默认值：null
        JsonBeanTemp enumjson = new Gson().fromJson(jsonEnum, JsonBeanTemp.class);
        System.out.println(enumjson.getPlanType());
        System.out.println(enumjson.toString());

    }
}


enum CustomEnumDateType {
    Day("dayType"), // json 可以解析 Day ,不能解析自定义的 dayType.
    Weak("weakType");

    private String dateType;

    CustomEnumDateType(String dateType) {
        this.dateType = dateType;
        System.out.println("CustomEnumDateType:=="+dateType);//CustomEnumDateType:==dayType   CustomEnumDateType:==weakType
    }

    public static String getType(CustomEnumDateType date) {
        return date.dateType;
    }

}

enum PlanType {
    CYCLE((byte) 0), TIMING((byte) 2), NOW((byte) 3), IINTERVALCYCLE((byte) 1), MAT((byte) 4);
    private byte value;

    PlanType(byte value) {
        this.value = value;
    }

    public byte value() {
        return this.value;
    }

    // 普通方法
    public static byte getByteType(String str) {
        for (PlanType c : PlanType.values()) {
            if (c.name().equals(str)) {
                return c.value;
            }
        }
        return 0;
    }
}

class JsonBeanTemp {
    String a;
    CustomEnumDateType myDate;
    PlanType planType;

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public CustomEnumDateType getMyDate() {
        return myDate;
    }

    public void setMyDate(CustomEnumDateType myDate) {
        this.myDate = myDate;
    }

    public PlanType getPlanType() {
        return planType;
    }

    public void setPlanType(PlanType planType) {
        this.planType = planType;
    }

    @Override
    public String toString() {
        return "enumTest.JsonBeanTemp [a=" + a + ", myDate=" + myDate + ", planType=" + planType + "]";
    }

}